import matplotlib as mat
import csv
import numpy as np
import matplotlib.pyplot as plt
from math import *


xge=1
yge=1
tmax=1e4
xindex=5
xstep=0.05
yindex=11

ystep=0.3
xlog=True
ylog=False

# % 1  2   3  4   5  6   7   8   9  10 11 12  13  14 15  16  17  18  ----1!!!!!!!!!!!!
# % m1,m2,Lx,ecc0,a,tb0,mt1,mt2,kw,kw2,mx,mx2,eccx,tbx,f,nnn,uuu,m_tr
# % m1,m2,Lx,ecc0,a,tb0,mt1,mt2,kw,kw2,mx,mx2,eccx,tbx,f,m_transferrate,b_iso,m_tr
name='first_Lx39erg_z0.02_rb'
resultnp=np.load("".join([name,'.npy']))
kw2=resultnp[:,9]
NS=resultnp[kw2==13,:]
BH=resultnp[kw2==14,:]
# jianNum=100
Tjian=1
NStwei=np.ceil((NS[:,6])/Tjian)
BHtwei=np.ceil((BH[:,6])/Tjian)
NStwei=NStwei.astype(np.int)
BHtwei=BHtwei.astype(np.int)
pltxT=np.array(range(1,10000,Tjian))
tdata=np.zeros((2,np.size(pltxT)), dtype = np.int) 

for wei in NStwei:
    tdata[0,wei]=tdata[0,wei]+1
for wei in BHtwei:
    tdata[1,wei]=tdata[1,wei]+1
# tdata 0 NS   1 BH  
tdataCum=np.cumsum(tdata,axis=1)
ax1=plt.figure()
plt.loglog(pltxT,tdata[0,:])

plt.loglog(pltxT,tdata[1,:])
# plt.semilogy(y, 'o-', c='orangered',
#             label='semilog')
plt.show()

plt.figure()
plt.plot(pltxT,tdataCum[0,:])
plt.plot(pltxT,tdataCum[1,:])
plt.show()
xdata=resultnp[:,xindex]
ydata=resultnp[:,yindex]
timespan=resultnp[:,7]-resultnp[:,6]
if(xlog):
        xdata=np.log10(xdata)
if(ylog):
        ydata=np.log10(ydata)
x=min(xdata)
y=min(ydata)
xrange=[]
xrangestr=[]
yrangestr=[]
yrange=[]
while (x<max(xdata)):
    xrange.append(x)
    xrangestr.append("%.2f" % x)
    x=x+xstep*xge
while(y<max(ydata)):
    yrange.append(y)
    yrangestr.append("%.2f" % y)
    y=y+ystep*yge


datamap=np.zeros((len(xrange)*xge,len(yrange)*yge))
xnpdata=np.array(xdata)
ynpdata=np.array(ydata)
xwei=((xnpdata-min(xdata))/xstep).astype(int)
ywei=((ynpdata-min(ydata))/ystep).astype(int)
for i in range(len(xdata)):
    if(timespan[i]>0.10):continue
    datamap[xwei[i]][ywei[i]]=datamap[xwei[i]][ywei[i]]+timespan[i]


# searchxls=xlrd.open_workbook("search20200325beta1,xls")

datamap=datamap/(np.max(datamap))




fig, ax = plt.subplots()
ax.imshow(datamap, interpolation='nearest',origin ='lower')
plt.xticks(range(1,np.max(ywei),xge),yrangestr)
plt.yticks(range(1,np.max(xwei),yge),xrangestr)

# plt.xlabel('campanion mass / Msun')
# plt.ylabel('orbit period / day')

plt.xlabel('m1 / Msun')
plt.ylabel('m2 / Msun')
plt.title('')
plt.show()
